@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="card-title mb-0">材料列表</h5>
                    @if(Auth::user()->hasRole('admin'))
                    <div>
                        <a href="{{ route('materials.create') }}" class="btn btn-success btn-sm">
                            <i class="bi bi-plus-circle"></i> 添加材料
                        </a>
                        <a href="{{ route('materials.export') }}" class="btn btn-info btn-sm">
                            <i class="bi bi-download"></i> 导出
                        </a>
                    </div>
                    @endif
                </div>
                <div class="card-body">
                    @if (session('success'))
                        <div class="alert alert-success">
                            {{ session('success') }}
                        </div>
                    @endif

                    <form action="{{ route('materials.index') }}" method="GET" class="mb-4">
                        <div class="row g-3">
                            <div class="col-md-4">
                                <input type="text" class="form-control" name="search" 
                                    placeholder="搜索材料名称、用途或备注" value="{{ request('search') }}">
                            </div>
                            <div class="col-md-3">
                                <select name="type_id" class="form-select">
                                    <option value="">全部类型</option>
                                    @foreach($types as $type)
                                        <option value="{{ $type->id }}" 
                                            {{ request('type_id') == $type->id ? 'selected' : '' }}>
                                            {{ $type->name }}
                                        </option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-2">
                                <button type="submit" class="btn btn-primary w-100">搜索</button>
                            </div>
                            <div class="col-md-2">
                                <a href="{{ route('materials.index') }}" class="btn btn-secondary w-100">重置</a>
                            </div>
                        </div>
                    </form>

                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead class="table-light">
                                <tr>
                                    <th>ID</th>
                                    <th>名称</th>
                                    <th>类型</th>
                                    <th>单位</th>
                                    <th>库存</th>
                                    <th>用途</th>
                                    <th>备注</th>
                                    @if(Auth::user()->hasRole('admin'))
                                    <th>操作</th>
                                    @endif
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($materials as $material)
                                    <tr class="{{ $loop->even ? 'table-light' : '' }}">
                                        <td>{{ $material->id }}</td>
                                        <td>{{ $material->name }}</td>
                                        <td>{{ $material->type->name }}</td>
                                        <td>{{ $material->unit }}</td>
                                        <td>
                                            <span class="badge bg-{{ $material->quantity <= 10 ? 'danger' : 'success' }}">
                                                {{ $material->quantity }}
                                            </span>
                                        </td>
                                        <td>{{ $material->purpose }}</td>
                                        <td>{{ $material->remark }}</td>
                                        @if(Auth::user()->hasRole('admin'))
                                        <td>
                                            <a href="{{ route('materials.edit', $material->id) }}" 
                                               class="btn btn-primary btn-sm">
                                                <i class="bi bi-pencil"></i> 编辑
                                            </a>
                                            <button type="button" class="btn btn-danger btn-sm" 
                                                    onclick="confirmDelete('{{ route('materials.destroy', $material->id) }}')">
                                                <i class="bi bi-trash"></i> 删除
                                            </button>
                                        </td>
                                        @endif
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>

                    <div class="d-flex justify-content-center mt-4">
                        {{ $materials->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>确定要删除这个材料吗？此操作不可恢复。</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteForm" method="POST" style="display: inline;">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="btn btn-danger">确认删除</button>
                </form>
            </div>
        </div>
    </div>
</div>

@endsection

@push('scripts')
<script>
function confirmDelete(url) {
    $('#deleteForm').attr('action', url);
    $('#deleteModal').modal('show');
}
</script>
@endpush 